package com.zr.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.User;

public class LoginFilter implements Filter{
    public LoginFilter(){
    	 
    	
    }
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("过滤器被销毁了");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("进入过滤器");
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse resp =(HttpServletResponse) response;
		req.setCharacterEncoding("utf8");
		HttpSession session=req.getSession();
		
		if(null==session.getAttribute("currentUser")){
		    resp.sendRedirect("../login.jsp");
		}else {
			filterchain.doFilter(req, resp);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
